﻿Type=Activity
Version=3.15
@EndOfDesignText@
#Region Module Attributes
	#FullScreen: False
	#IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	Dim btnNew As Button

	Dim lstZDXM As List
	Dim spnZCXM As Spinner
	Dim dalRQ As DateDialog
	Dim edtZCRQ As EditText
	Dim btnSelZCRQ As Button
	Dim edtDJ As EditText
	Dim edtSL As EditText
	Dim edtJE As EditText
	Dim edtBZ As EditText
	Dim btnSave As Button
	Dim btnImg As Button
	
	Dim FZCID As Int
	Dim FID As Long
	Dim FTitle As String
	
	
	FTitle = "支出记录"
	
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("FormJZZCJL")
	Activity.Title = FTitle '"支出记录"	

	InitLayOut
	InitData_ZCXM
	Clear
	
End Sub

Sub Activity_Resume
	LoadDataByID(PublicVar.GV_JZ_ZCJL_ID)

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub InitLayOut
	'高级菜单
	Activity.AddMenuItem("支出查询","MenuItemAdv")
End Sub

Sub InitData_ZCXM
	Dim DataSetZCXM As Cursor
	Dim sSQL As String
	
	lstZDXM.Initialize
	
	sSQL = "SELECT BM,MC,DW,DJ FROM ZD_ZCXM ORDER BY BM"
	Log("SQL: " & sSQL)
	DataSetZCXM = PublicVar.GV_SQLTool.ExecQuery(sSQL)
	
	For i = 0 To DataSetZCXM.RowCount - 1
		DataSetZCXM.Position = i
		Dim RecMap As Map
		RecMap.Initialize
		RecMap.Put("BM",DataSetZCXM.GetString("BM"))
		RecMap.Put("MC",DataSetZCXM.GetString("MC"))
		RecMap.Put("DW",DataSetZCXM.GetString("DW"))
		RecMap.Put("DJ",DataSetZCXM.GetDouble("DJ"))
		
		lstZDXM.Add(RecMap)
	Next
	DataSetZCXM.Close

	spnZCXM.Clear
	For i = 0 To lstZDXM.Size -1 
		RecMap = lstZDXM.get(i)
		spnZCXM.Add(RecMap.get("BM") & " " &RecMap.get("MC"))
		
	Next
End Sub


Sub spnZCXM_ItemClick (Position As Int, Value As Object)
	Dim sZCXM,sBM,sMC As String
	Dim iIndex As Int
	Dim RecMap As Map
		
	sZCXM = GetZCXMBM(Value)
	
	If sZCXM <> "" Then
		sBM = sZCXM
	Else
		ToastMessageShow("没有可选的支出项目", True)	
		Return	
	End If
	
	RecMap = GetZCXMRecMap(sBM)
	edtDJ.Text = RecMap.get("DJ")
	
End Sub

Sub GetZCXMBM(AText As String) As String
	Dim iIndex As Int
	
	iIndex = AText.IndexOf(" ")
	If iIndex > 0 Then
		Return AText.SubString2(0,iIndex)
	Else
		Return ""
	End If
		
End Sub

Sub GetZCXMRecMap(ABM As String) As Map
	Dim RecMap As Map
	For i =0 To lstZDXM.Size -1
		RecMap = lstZDXM.get(i) 
		If RecMap.get("BM") = ABM Then
			Return RecMap	
		End If
	Next

End Sub

Sub edtDJ_TextChanged (Old As String, New As String)

	If Old<>New Then
		CalcJE
			
	End If
End Sub

Sub edtSL_TextChanged (Old As String, New As String)
	If Old<>New Then
		CalcJE
			
	End If
End Sub

Sub CalcJE
	Dim fDJ,fSL,fJE As Double
	fDJ = 0
	fSL = 0
	If IsNumber(edtDJ.Text) Then fDJ = edtDJ.Text
	If IsNumber(edtSL.Text) Then fSL = edtSL.Text
	
	fJE = fDJ * fSL
	edtJE.Text = fJE
End Sub

Sub btnSelZCRQ_Click
	Dim sDate,sDate2 As String
	Dim iDate As Long
	Dim iIndex,iRet As Int
	
	If edtZCRQ.Text <>"" Then
		sDate = edtZCRQ.Text	
		DateTime.DateFormat = "yyyy-MM-dd"
		iDate = DateTime.DateParse(sDate)
	Else
		iDate = DateTime.Now
	End If
	
	dalRQ.Year = DateTime.GetYear(iDate)
	dalRQ.Month = DateTime.GetMonth(iDate)
	dalRQ.DayOfMonth = DateTime.GetDayOfMonth(iDate)	
	
	iRet = dalRQ.Show("","选择支出日期","确定","取消","",Null)
	If iRet = -1 Then
		DateTime.DateFormat = "yyyy-MM-dd"
		edtZCRQ.Text = DateTime.Date(dalRQ.DateTicks) 
	End If
End Sub

Sub btnSave_Click
	If CheckBeforeSave = False Then	Return
	
	If SaveToDB = False Then Return
	
	ToastMessageShow("保存成功", True)
	
	btnNew_Click
	
End Sub

Sub CheckBeforeSave As Boolean
	
	If edtZCRQ.Text = "" Then
		edtZCRQ.RequestFocus
		ToastMessageShow("请选择支出日期", True)
		Return False			
	End If
	
	If spnZCXM.SelectedIndex = -1 Then
		spnZCXM.RequestFocus
		ToastMessageShow("请选择支出项目", True)
		Return False			
	End	If
	
	If edtJE.Text = "" Then
		edtJE.RequestFocus
		ToastMessageShow("请录入金额", True)
		Return False			
	End If
	
	Return True
End Sub

Sub SaveToDB As Boolean
	Dim iNow,iZCRQ As Long
	Dim sDate,sXMBM,sBZ,sSQL As String
	Dim fDJ,fSL,fJE As Double
		
	PublicVar.GV_SQLTool.BeginTransaction
	Try
		sDate = edtZCRQ.Text
		DateTime.DateFormat = "yyyy-MM-dd"
		iZCRQ = DateTime.DateParse(sDate)

		DateTime.DateFormat = "yyyy-MM-dd"
		iNow = DateTime.Now
		
		sXMBM = spnZCXM.SelectedItem
		sXMBM = GetZCXMBM(sXMBM)
		
		fDJ = 0
		fSL = 0
		fJE = 0
		If IsNumber(edtDJ.Text) Then fDJ = edtDJ.Text
		If IsNumber(edtSL.Text) Then fSL = edtSL.Text
		If IsNumber(edtJE.Text) Then fJE = edtJE.Text
		
		sBZ = edtBZ.Text
		
		If FID = 0 Then  '新增			
			sSQL = "SELECT MAX(ID) + 1 from JZ_ZCJL0 "
			Log("SQL: " & sSQL)
			Try
				FID = PublicVar.GV_SQLTool.ExecQuerySingleResult(sSQL)
			Catch
				FID =1
			End Try
			
			sSQL = "INSERT INTO JZ_ZCJL0(ZCRQ,XMBM,DJ,SL,JE,BZ,LRRY,LRRQ,_FLAG,ID)"
			sSQL = sSQL & " VALUES('"&sDate&"',"&sXMBM&","&fDJ&","&fSL&","&fJE&",'"&sBZ&"','"&PublicVar.GV_UserCode&"',"
			sSQL = sSQL &"'"&DateTime.Date(iNow) & " "&DateTime.Time(iNow) &"',0,"&FID&")"
			
			
		Else '修改
			sSQL = "UPDATE JZ_ZCJL0 SET ZCRQ='"&sDate&"',XMBM="&sXMBM &",DJ="&fDJ &"," 
			sSQL = sSQL & "SL="&fSL&",JE="&fJE&",BZ='"&sBZ&"',_FLAG =0 "
			sSQL = sSQL & "WHERE ID="&FID
		
		End If
		
		Log("SQL: " & sSQL)
		PublicVar.GV_SQLTool.ExecNonQuery(sSQL)
		
		PublicVar.GV_SQLTool.TransactionSuccessful
	Catch
		Log(LastException.Message)
		ToastMessageShow("保存失败"&CRLF&LastException.Message, True)	
		Return
	End Try
	PublicVar.GV_SQLTool.EndTransaction
	Return True
End Sub

Sub MenuItemAdv_Click
	If Sender = "支出查询" Then
		StartActivity("JZ_ZCJLView")
	End If
	
End Sub

Sub Clear
	edtZCRQ.Text = ""
	DateTime.DateFormat = "yyyy-MM-dd"
	edtZCRQ.Text = DateTime.Date(DateTime.Now)
	edtDJ.Text = ""
	edtSL.Text = ""
	edtJE.Text = ""
	edtBZ.Text = ""
	 
	FZCID = 0 'Server DB 的ID
	FID = 0 '0 insert >0 modify
	
End Sub
Sub btnNew_Click
	Clear
	PublicVar.GV_JZ_ZCJL_ID = 0
	SetTitleAndButton
End Sub

Sub SetTitleAndButton
	Activity.Title = FTitle & "[记录号:"&FID&"]"
	
End Sub

Sub LoadDataByID(AID As Long)
	Dim sSQL,sZCXM As String
	Dim DataSetZCJL As Cursor
	Dim iIndex As Int
	
	If AID >0 Then
		FID = AID
			
		sSQL = "SELECT JL.*,XM.MC AS XMMC   "
		sSQL = sSQL & "FROM JZ_ZCJL0 JL " 
		sSQL = sSQL & "LEFT JOIN ZD_ZCXM XM ON JL.XMBM=XM.BM " 	
		sSQL = sSQL & "WHERE ID = " & FID
		
		DataSetZCJL = PublicVar.GV_SQLTool.ExecQuery(sSQL)		
		If DataSetZCJL.RowCount>0 Then
			DataSetZCJL.Position = 0
			
			edtZCRQ.Text = DataSetZCJL.GetString("ZCRQ")			
			edtDJ.Text = DataSetZCJL.GetString("DJ")
			edtSL.Text = DataSetZCJL.GetString("SL")
			edtJE.Text = DataSetZCJL.GetString("JE")
			edtBZ.Text = DataSetZCJL.GetString("BZ")
			
			sZCXM = DataSetZCJL.GetString("XMBM") & " " & DataSetZCJL.GetString("XMMC")
			iIndex = spnZCXM.IndexOf(sZCXM)
			If iIndex>-1 Then
				spnZCXM.SelectedIndex = iIndex	
			End If
			
		End If
		
	End If
	
	SetTitleAndButton

End Sub
Sub btnImg_Click
	StartActivity("JZ_Camera")
End Sub